module WWW
  class Tkd
    class GwtBuyerOrder
      attr_reader :order,:express,:status
      attr_writer :order,:express,:status
      def to_json(*args)
        {
          'order'  => @order,
          'express'        => @express,
          'status'        => @status,
        }.to_json(*args)
      end
      #通过订单信息初始化其快递信息
      def self.init_by_order buyer_order
        view = self.new
        view.order = WWW::Tkd::TbOrder.find buyer_order.order_id
        view.express = WWW::Tkd::Express.find_by_id(buyer_order.express_id)
        if view.express!=nil
          view.status = find_status(view.express)
        end
        return view
      end
      def self.find_status express
        return WWW::Tkd::Status.find_form_agent_id_and_no(express.agent_id,express.no_code)
      end
      #保存订单信息
      def self.save buyer_order,order,express,user_id
        #确认该记录是该用户的记录
        db_express = WWW::Tkd::Express.find_by_agent_id_and_no_code(express.agent_id,express.no_code)
        #新建express对象
        if db_express==nil
          db_express= WWW::Tkd::Express.new
          db_express.agent_id=express.agent_id
          db_express.no_code=express.no_code
          db_express.is_new=0
          db_express.not_exist=0
          db_express.is_sync=0
          db_express.is_end=0
          db_express.support_status =1
          db_express.save
        end
        #更新tb_buyer_orders对象
        buyer_order.express_id = db_express.id
        buyer_order.save
      end
    end
  end
end
